<template>
	<view class="container">
		<!-- #ifdef H5 -->
		<uni-header></uni-header>
		<!-- #endif -->

		<view class="order-box com-box">
			<view class="navbar">
				<view v-for="(item, index) in navList" :key="index" class="nav-item" :class="{current: tabCurrentIndex === index}"
				 @click="tabClick(index)">
					{{item.text}}
				</view>
			</view>

			<swiper :current="tabCurrentIndex" class="swiper-box" duration="300" @change="changeTab">
				<swiper-item class="tab-content" v-for="(tabItem,tabIndex) in navList" :key="tabIndex">
					<scroll-view class="list-scroll-content" scroll-y @scrolltolower="loadData">
						<!-- 空白页 -->
						<empty v-if="tabItem.loaded === true && tabItem.orderList.length === 0"></empty>
						<!-- 订单列表 -->
						<view v-for="(item,index) in tabItem.orderList" :key="index" class="order-item">
							<navigator class="link" :url="'/pages/order/detail?orderid=' + item.id">
								<view class="i-top">
									<text class="time">{{$util.sliceTime(item.createTime)}}</text>
									<text class="state">{{statusMap[item.status]}}</text>
								</view>

								<view class="goods-box-single">
									<image class="goods-img" :src="item.goodsPic" mode="aspectFill"></image>
									<view class="right">
										<text class="title clamp">{{item.goodsName}}</text>
										<text class="attr-box">{{item.specification}} x {{item.buyCount}}</text>
										<text class="price">{{item.price}}</text>
									</view>
								</view>
								<view class="price-box">
									共
									<text class="num">{{item.buyCount}}</text>
									件商品 实付款
									<text class="price">{{item.price}}</text>
								</view>
							</navigator>

							<view class="action-box" v-if="item.status == 1">
								<button type="primary" :disabled="submiting" class="action-btn default" @click="cancelOrder(item)">取消订单</button>
								<button type="primary" :disabled="submiting" class="action-btn" @click="payOrder(item)">立即支付</button>
							</view>
							<view class="action-box" v-if="item.status == 2">
								<button type="primary" :disabled="submiting" class="action-btn default" @click="refundOrder(item)">申请退款</button>
							</view>
							<view class="action-box" v-if="item.status == 4">
								<button v-if="item.driverId" type="primary" class="action-btn spec" @click="showDriverInfo(item)">查看司机</button>
								<button v-if="item.ordersn" type="primary" class="action-btn spec" @click="showShipTrace(item)">查看物流</button>
								<button type="primary" :disabled="submiting" class="action-btn" @click="confirmOrder(item)">确认收货</button>
							</view>
							<view class="action-box" v-if="item.status == 5">
								<button type="primary" class="action-btn" @click="appraiseOrder(item)">立即评价</button>
							</view>
							<view class="action-box" v-if="item.status == 10">
								<button type="primary" class="action-btn default">订单已取消</button>
							</view>
						</view>
						<uni-load-more :status="tabItem.loadingType"></uni-load-more>
					</scroll-view>
				</swiper-item>
			</swiper>
			<uni-modal-next :show="refundShow" @close="refundShow = false" title="退款" @cancel="refundShow = false" @confirm="refundConfirm">
				<textarea v-model="inputRefundReason" style="padding: 30upx 20upx" placeholder="简要描述退款理由.." />
			</uni-modal-next>
		</view>

		<!-- #ifdef H5 -->
		<uni-footer></uni-footer>
		<!-- #endif -->
	</view>
</template>

<script>
	import uniModalNext from '@/components/uni-modal-next.vue'
	import uniLoadMore from '@/components/uni-load-more.vue'
	import empty from "@/components/empty.vue"
	import uniHeader from '@/components/h5/uni-header.vue'
	import uniFooter from '@/components/h5/uni-footer.vue'
	export default {
		components: {
			uniLoadMore,
			empty,
			uniModalNext,
			uniHeader,
			uniFooter
		},
		data() {
			return {
				statusMap: {
					0: '全部',
					1: '待付款',
					2: '待发货',
					4: '运输中',
					5: '已完成',
					8: '售后'
				},
				submiting: false,
				refundShow: false,
				inputRefundReason: '',
				refundOrderItem: '',
				tabCurrentIndex: 0,
				navList: [{
						state: 0,
						text: '全部',
						loadingType: 'more',
						pageNum: 1,
						pageSize: 10,
						pages: 0,
						orderList: []
					}, {
						state: 1,
						text: '待付款',
						loadingType: 'more',
						pageNum: 1,
						pageSize: 10,
						pages: 0,
						orderList: []
					}, {
						state: 2,
						text: '待发货',
						loadingType: 'more',
						pageNum: 1,
						pageSize: 10,
						pages: 0,
						orderList: []
					}, {
						state: 4,
						text: '待收货',
						loadingType: 'more',
						pageNum: 1,
						pageSize: 10,
						pages: 0,
						orderList: []
					}, {
						state: 5,
						text: '待评价',
						loadingType: 'more',
						pageNum: 1,
						pageSize: 10,
						pages: 0,
						orderList: []
					}, {
						state: 8,
						text: '退款中',
						loadingType: 'more',
						pageNum: 1,
						pageSize: 10,
						pages: 0,
						orderList: []
					}
				],
			}
		},
		onLoad(option) {
			this.tabCurrentIndex = 0
			for (let i = 0; i < this.navList.length; i++) {
				if (this.navList[i].state === parseInt(option.state)) {
					this.tabCurrentIndex = i
				}
			}
			this.loadData()
		},
		methods: {
			//获取订单列表
			loadData(source) {
				//这里是将订单挂载到tab列表下
				let index = this.tabCurrentIndex
				let navItem = this.navList[index]
				let state = navItem.state
				if (source === 'tabChange' && navItem.loaded === true) {
					//tab切换只有第一次需要加载数据
					return
				}
				if (navItem.loadingType === 'loading') {
					//防止重复加载
					return
				}
				navItem.loadingType = 'loading'
				let orderList = this.$api.request('/userOrder/selectOrderByStatus', {
					status: navItem.state,
					pageNum: navItem.pageNum,
					pageSize: navItem.pageSize
				}, {
					method: 'GET'
				}).then(res => {
					navItem.pages = Math.ceil(res.data.total / navItem.pageSize)
					navItem.loadingType = navItem.pageNum < navItem.pages ? 'more' : 'nomore'
					navItem.pageNum = navItem.pageNum + 1
					res.data.records.forEach(item => {
						navItem.orderList.push(item)
					})
					//loaded新字段用于表示数据加载完毕，如果为空可以显示空白页
					this.$set(navItem, 'loaded', true)
				})
			},
			//swiper 切换
			changeTab(e) {
				this.tabCurrentIndex = e.target.current
				this.loadData('tabChange')
			},
			//顶部tab点击
			tabClick(index) {
				this.tabCurrentIndex = index
			},
			payOrder(item) {
				uni.navigateTo({
					url: '/pages/pay/pay?allOrder=' + item.orderNum + '&price=' + item.price
				})
			},
			//取消订单
			cancelOrder(item) {
				uni.showModal({
					title: '取消?',
					content: '您确定要取消此订单吗?',
					success: (e) => {
						if (e.confirm) {
							this.submiting = true
							this.$api.request('/userOrder/cancelOrder', {
								orderId: item.id
							}, {
								content: 'application/x-www-form-urlencoded',
								failback: failres => {
									this.submiting = false
									this.$api.msg(failres.message)
								}
							}).then(res => {
								this.submiting = false
								item.status = 10
							})
						}
					}
				})
			},
			//订单退款
			refundConfirm() {
				if (this.submiting) {
					return
				}
				this.submiting = true
				this.$api.request('/userOrder/waitSendOrderRefund', {
					orderId: this.refundOrderItem.id,
					refundReason: this.inputRefundReason
				}, {
					content: 'application/x-www-form-urlencoded',
					failback: failres => {
						this.submiting = false
						this.$api.msg(failres.message)
					}
				}).then(res => {
					this.submiting = false
					this.refundOrderItem.status = 8
					this.$api.msg('申请退款成功！')
				})
			},
			refundOrder(item) {
				this.refundOrderItem = item
				this.refundShow = true
				this.inputRefundReason = ''
			},
			//确认订单
			confirmOrder(item) {
				uni.showModal({
					title: '收货?',
					content: '您确定要确认收货吗?',
					success: (e) => {
						if (e.confirm) {
							this.submiting = true
							this.$api.request('/userOrder/transittingOrder', {
								orderId: item.id,
								type: 1
							}, {
								content: 'application/x-www-form-urlencoded',
								failback: failres => {
									this.submiting = false
									this.$api.msg(failres.message)
								}
							}).then(res => {
								this.submiting = false
								item.status = 5
							})
						}
					}
				})
			},
			showShipTrace(item) {
				uni.navigateTo({
					url: "/pages/order/trace?allOrder=" + item.orderNum
				})
			},
			showDriverInfo(item) {
				uni.navigateTo({
					url: "/pages/order/driver?allOrder=" + item.orderNum
				})
			},
			//评价订单
			appraiseOrder(item) {
				uni.navigateTo({
					url: '/pages/order/appraise?id=' + item.id
				})
			}
		}
	}
</script>

<style lang="scss">
	page,
	.order-box,
	.container {
		height: 100%;
	}

	.container {
		padding-top: 80upx;
	}

	.swiper-box,
	.list-scroll-content {
		height: 100%;
	}

	.uni-swiper-item {
		height: auto;
	}

	/* #ifdef H5 */
	@media screen and (min-width: 750px) {

		page,
		.order-box,
		.container {
			height: auto;
		}

		.container {
			padding-top: 0;
		}

		.order-box {
			margin: 20px auto;
		}

		.navbar {
			background-color: #e0edff;
		}

		.swiper-box {
			height: 100vh;
			background-color: #fff;
		}

		.list-scroll-content {
			height: 100vh;
		}

		.uni-swiper-item {
			height: auto;
		}

	}

	/* #endif */
</style>
